package com.wondersgroup.fjzlpt.org.dao.impl.jtqy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.wondersgroup.fjzlpt.org.dao.inter.jtqy.YbfwxgxDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 医保服务项关系表--实现层
 * Created by GZB on 2017/11/22.
 */
@Repository
public class YbfwxgxDaoImpl extends BaseJTDAOImpl implements YbfwxgxDao {

    @Override
    @Autowired
    @Qualifier("TB_JTQY_YBFWXGX")
    public void setTb(TableBean tb) {
        super.setTb(tb);
    }

    /**
     * 查询医保服务项列表
     * @param map
     * @return
     */
    @Override
    public JSONArray queryList(Map<String, String> map){
        JSONObject sqlObj = getBaseQuerySql(map);
        JSONArray sqlParams = sqlObj.getJSONArray("sqlParams");

        SQLBean bean = new SQLBean(sqlObj.getString("sql"), sqlParams.toArray());
        return super.queryList(bean);
    }

    /**
     * 合成基本的查询语句
     * @return
     */
    private JSONObject getBaseQuerySql(Map<String,String> map){
        StringBuffer sqlBuf = new StringBuffer();
        List sqlParams = new ArrayList();
        sqlBuf.append("select a.*,b.FWMC from ").append(this.tb.getTableName()).append(" a ");
        sqlBuf.append("left join TB_XXWH_FWXX b on a.FWID=b.FWID where 1=1 ");
        String value = map.get("QYXXID");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.QYXXID=? ");
            sqlParams.add(value);
        }


        JSONObject result = new JSONObject();
        result.put("sql",sqlBuf.toString());
        result.put("sqlParams",sqlParams);
        return result;
    }

    /**
     * 批量插入
     * @param map
     */
    public void insertBatch(Map<String, String> map){
        String YBFWX_ARR_Str = map.get("YBFWX_ARR");
        if(!StringUtils.isEmpty(YBFWX_ARR_Str)){
            String QYXXID = map.get("QYXXID");
            JSONArray YBFWX_ARR = JSONArray.parseArray(YBFWX_ARR_Str);
            List<Map> listMap = new ArrayList<Map>();
            for(int i=0;i<YBFWX_ARR.size();i++){
               JSONObject item = YBFWX_ARR.getJSONObject(i);
               item.put("QYXXID", QYXXID);
               listMap.add(item);
            }
            super.insertBatch(listMap);
        }
    }

    /**
     * 批量删除
     * @param map
     */
    public void deleteBatch(Map<String, String> map){
        String QYXXID = map.get("QYXXID");
        if(!StringUtils.isEmpty(QYXXID)){
            StringBuffer sqlBuf = new StringBuffer();
            sqlBuf.append("delete ").append(this.tb.getTableName()).append(" where QYXXID=? ");

            SQLBean bean = new SQLBean(sqlBuf.toString(), QYXXID);
            super.update(bean);
        }
    }

    /**
     * 根据服务ID 查询医保服务的服务次数是否已经全部完成
     * @param FWID
     * @return
     */
    public boolean isExcel(String FWID){
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select count(1) from \n");
        //查询某医保服务项的总服务次数
        sqlBuf.append("(select FWID,sum(FWCS) ZFWCS from TB_JTQY_YBFWXGX group by FWID) a \n");
        sqlBuf.append("left join \n");
        //查询某医保服务项的总完成次数（FWZT=1 && FWID=FWBID)
        sqlBuf.append("(select FWID,sum(case when FWZT=1 and FWID=FWBID then 1 else 0 end) YWCFWCS from TB_HZGL_FPFW group by FWID) b on a.FWID=b.FWID \n");
        sqlBuf.append("where a.ZFWCS<>(case when b.YWCFWCS is null then 0 else b.YWCFWCS end) and a.FWID = ? ");

        SQLBean bean = new SQLBean(sqlBuf.toString(), FWID);
        return super.isExist(bean);
    }
}
